/**
 *    @file       signals.cpp
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       02/11/2014 11:25:14 AM
 *
 *    @author     Theophilus (),  wangtf418@gmail.com
 */
#include "signals.h"

signals::signals()
{
	clear();
	sigset_t sigset;
	sigemptyset(&sigset);
	sigemptyset(&m_oldmaskset);
	sigprocmask(SIG_BLOCK, &sigset, &m_oldmaskset);
}

signals::~signals()
{

}

bf_int_t signals::addall()
{
	if (sigfillset(&m_sigset) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::add(bf_int_t signum)
{
	if (sigaddset(&m_sigset, signum) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::del(bf_int_t signum)
{
	if (sigdelset(&m_sigset, signum) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::if_in_set(bf_int_t signum)
{
	bf_int_t ret = sigismember(&m_sigset, signum);
	if (ret > 0)
	{
		return BF_TRUE;
	}
	else if (ret == 0)
	{
		return BF_FALSE;
	}
	else
	{
		return BF_ERROR;
	}
}

bf_int_t signals::clear()
{
	if (sigemptyset(&m_sigset) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::set_block_signals()
{
	if (sigprocmask(SIG_SETMASK, &m_sigset, NULL) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::add_block_signals()
{
	if (sigprocmask(SIG_BLOCK, &m_sigset, NULL) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::del_block_signals()
{
	if (sigprocmask(SIG_UNBLOCK, &m_sigset, NULL) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::recover_block_signals()
{
	if (sigprocmask(SIG_SETMASK, &m_oldmaskset, NULL) == 0)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

//static/////////////////////////
bf_int_t signals::set_sighandler(bf_int_t signum, sighandler_t handler)
{
	if (signal(signum, handler) != SIG_ERR)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::set_default_sighandler(bf_int_t signum)
{
	if (signal(signum, SIG_DFL) != SIG_ERR)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

bf_int_t signals::ignore_signal(bf_int_t signum)
{
	if (signal(signum, SIG_IGN) != SIG_ERR)
	{
		return BF_OK;
	}
	return BF_ERROR;
}

